package com.cavas.domain.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cavas.domain.bean.Finance;
import com.cavas.domain.bean.Membre;
import com.cavas.domain.bean.Pret;
import com.cavas.domain.bean.Remboursement;

@Repository
public class DaoRemboursementImpl implements DaoRemboursement {

	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	public void addRemboursement(Remboursement rbm, Pret pret) {
		Session session = sessionFactory.getCurrentSession();
		session.save(rbm);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Remboursement> allRemboursement() {
		return sessionFactory.getCurrentSession().createQuery("from Remboursement").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Remboursement> allRemboursement(String nom) {

		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Remboursement clt where clt.nom = :nom");
		query.setParameter("nom", nom);
		return  query.list();
	}
	
	
	@Override
	public void deleteRemboursement(int id) {
		Session session = sessionFactory.getCurrentSession();
		Remboursement rmb = (Remboursement) session.get(Remboursement.class, id);
		if(rmb != null){
			/*Query query = session.createQuery("from Finance clt where clt.date = :tdate");
		List<Finance> liste = query.list();
		for(Finance finance : liste)
			session.delete(finance);*/
		session.delete(rmb);	
		}
	}
	
	@Override
	public void updateRemb(Remboursement rbm) {

	}

	@Override
	public int getTotalRemboursement() {
		List<Remboursement> list = this.allRemboursement();
		int total = 0;
		
		for(Remboursement pret : list){
			total = total + pret.getMontant();
		}
		return total;
	}

	@Override
	public int getTotalInteret() {
		List<Remboursement> liste = this.allRemboursement();
		int total = 0;
		for(Remboursement rbm: liste){
			total = total + rbm.getInteret();
		}
		return total;
	}
	
	
	@Override
	public void calcolateInteretAndReste(Remboursement rbm){
		Pret p = rbm.getPret();
		List<Remboursement> liste = this.getRemboursement(p);
		int interet = 0;
		
		int start_date = Integer.valueOf(p.getDate().split("/")[1]);
		int end_date = Integer.valueOf(rbm.getDate().split("/")[1]);
		if(start_date > end_date)
			end_date = start_date + 12;
		int duree = end_date - start_date;		
		int montant_rb = rbm.getMontant();
		
		Remboursement rb_tp = null;
		int reste_tp = p.getMontant();
		if(liste.size() > 0){
			rb_tp = liste.get(0);
			reste_tp = rb_tp.getReste();			
		}
		
		if(montant_rb >= reste_tp){
			interet = this.getInteret(reste_tp, duree, p.getExtra());
			rbm.setMontant(reste_tp);
			rbm.setReste(0);
			rbm.setInteret(interet);
			this.closePret(p);
		}
		else{
			interet = this.getInteret(montant_rb, duree, p.getExtra());
			rbm.setMontant(montant_rb - interet);
			int reste = reste_tp + interet - montant_rb;
			rbm.setReste(reste);
			rbm.setInteret(interet);		
		}
				
		rbm.setNom(p.getNom());
		
		return;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Pret getPret(int id_pret) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Pret clt where clt.pret = :idpret");
		query.setParameter("idpret", id_pret);
		List<Pret> liste = query.list();
		if(liste.size() > 0)
			return (Pret)liste.get(0);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Remboursement> getRemboursement(Pret pret){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Remboursement clt where clt.pret.pretId = :idpret");
		query.setParameter("idpret", pret.getPretId());
		return query.list();
	}
	
	
	private void closePret(Pret p){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Pret pt set pt.status =:statu"+" where pt.pretId =:id");
		query.setParameter("id", p.getPretId());
		query.setParameter("statu", "close");
		query.executeUpdate();
	}
	
	private int getInteret(int montant, int duree, String extra) {		
	    double percent = this.pourcentage(duree, extra);
	    double interet =  montant * percent;
		String s = String.valueOf(interet);
		int mt = (int) interet;
		
		String[] sl = s.split("\\.");
		String c = sl[1].substring(0,1);
		int i = Integer.parseInt(c);
		if(i >= 5)
			mt = mt + 1;
		return mt;
	}
	
	private double pourcentage(int duree, String extra){
		double perc;
		switch(duree){
			case 1:
				perc = 0.05;
				break;
			case 2:
				perc = 0.1;
				break;
			default:
				perc = 0.15;
		}
		
		if(!extra.equals("no"))
			perc = perc + 0.02;
		
		return perc;
	}

}
